import os
import sys
from argparse import ArgumentParser
from mgamedev.py_lib.decorator import singleton
from ccpm.pmbase import PluginBase
from ccpm.conffac import PluginConf
from ccpm.net import NetUtil


@singleton
class RelconfPlugin(PluginBase):

    PLUGIN_INFO = dict(
        name="relconf",
        help="rel conf to remote"
    )
    _pwd = ""
    _host = ""
    _isinit = False

    def __init__(self):
        super().__init__(self.PLUGIN_INFO)

    def __chk_format():
        # todo: Chk the conf fmt forbiden upload wrong global-config
        return True

    def parser_config(self, argparser):
        super().parser_config(argparser)
        argparser.add_argument("-c", dest="configfile",
                               help="lua-file for set global config for tool")

    def run(self, argpars):
        super().run(argpars)
        l_configf = argpars.configfile
        if not l_configf:
            self.logger.error("must input the confile for rel!")
            sys.exit(1)
        pwd = PluginConf().get("_sftp").pwd
        remote_path = PluginConf().get("_sftp").remote_path
        NetUtil.sftp_upload(pwd, l_configf, remote_path)
